﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EmployeeApp.UnitTest.EmployeeService;
using System.Collections.Generic;

namespace EmployeeApp.UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void checkCreateEmployeeInCase_InsertAllData()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            AddRequestDto employeeDTo = new AddRequestDto();

            employeeDTo.Address = "Test Address";
            employeeDTo.EmpId = "Test Id";
            employeeDTo.JobTitle = 1;
            employeeDTo.MarriedStatus = 1;
            employeeDTo.Name = "Test Name";
            employeeDTo.Status = 1;
            employeeDTo.Surname = "Test Surname";
            employeeDTo.Title = 1;

            // Act
            var result = ESC.Add(new AddRequest(employeeDTo));
            //var result = ESC.Add(employeeDTo);

            // Assert
            Assert.IsTrue(result.AddResult.Status, "Cannot insert new employee to Database");
            //Assert.IsTrue(result.Status, "Cannot insert new employee to Database");
        }

        [TestMethod]
        public void checkCreateEmployeeInCase_InsertSomeData()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            AddRequestDto employeeDTo = new AddRequestDto();

            int i = 1;

            while(i < 4)
            {
                employeeDTo.Address = "Test Address";
                employeeDTo.EmpId = "Test Id";
                employeeDTo.JobTitle = 1;
                employeeDTo.MarriedStatus = 1;
                employeeDTo.Name = "Test Name";
                employeeDTo.Status = 1;
                employeeDTo.Surname = "Test Surname";
                employeeDTo.Title = 1;

                switch (i)
                {
                    case 1: employeeDTo.EmpId = null; i++; break;
                    case 2: employeeDTo.Name = null; i++; break;
                    case 3: employeeDTo.Surname = null; i++; break;
                }

                // Act
                var result = ESC.Add(new AddRequest(employeeDTo));
                //var result = ESC.Add(employeeDTo);

                // Assert
                Assert.IsFalse(result.AddResult.Status, "It should cannot insert new employee to Database");
                //Assert.IsFalse(result.Status, "It should cannot insert new employee to Database");
            }
        }

        [TestMethod]
        public void checkCreateEmployeeInCase_InsertNullData()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            AddRequestDto employeeDTo = new AddRequestDto();

            // Act
            var result = ESC.Add(new AddRequest(employeeDTo));
            //var result = ESC.Add(employeeDTo);

            // Assert
            Assert.IsFalse(result.AddResult.Status, "It should cannot insert new employee to Database");
            //Assert.IsFalse(result.Status, "It should cannot insert new employee to Database");
        }

        [TestMethod]
        public void checkCreateEmployeeInCase_DuplicateEmployee()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            AddRequestDto employeeDTo = new AddRequestDto();

            employeeDTo.Address = "Test Address";
            employeeDTo.EmpId = "Test Id";
            employeeDTo.JobTitle = 1;
            employeeDTo.MarriedStatus = 1;
            employeeDTo.Name = "Test Name";
            employeeDTo.Status = 1;
            employeeDTo.Surname = "Test Surname";
            employeeDTo.Title = 1;

            // Act
            var result = ESC.Add(new AddRequest(employeeDTo));
            //var result = ESC.Add(employeeDTo);

            // Assert
            Assert.IsFalse(result.AddResult.Status, "Cannot insert duplicate employee to Database");
            //Assert.IsFalse(result.Status, "Cannot insert duplicate employee to Database");
        }

        [TestMethod]
        public void checkSearchEmployeeInCase_AllSearchField()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            GetAllRequestDto employeeDTo = new GetAllRequestDto();

            employeeDTo.EmpId = "Test Id";
            employeeDTo.Name = "Test Name";
            employeeDTo.Status = 1;
            employeeDTo.Sort = "Name";
            employeeDTo.SortType = "ASC";
            employeeDTo.Status = 1;

            // Act
            var result = ESC.GetAll(new GetAllRequest(employeeDTo));
            //var result = ESC.Add(employeeDTo);

            // Assert
            Assert.IsTrue(result.GetAllResult.Status, "Cannot search employee to Database");
            //Assert.IsFalse(result.Status, "Cannot insert duplicate employee to Database");
        }

        [TestMethod]
        public void checkSearchEmployeeInCase_SearchSomeField()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            GetAllRequestDto employeeDTo = new GetAllRequestDto();

            int i = 1;

            while (i < 7)
            {
                employeeDTo.EmpId = "Test Id";
                employeeDTo.Name = "Test Name";
                employeeDTo.Status = 1;
                employeeDTo.Sort = "Name";
                employeeDTo.SortType = "ASC";
                employeeDTo.Status = 1;

                switch (i)
                {
                    case 1: employeeDTo.EmpId = null; i++; break;
                    case 2: employeeDTo.Name = null; i++; break;
                    case 3: employeeDTo.Status = 1; i++; break;
                    case 4: employeeDTo.Sort = null; i++; break;
                    case 5: employeeDTo.SortType = null; i++; break;
                    case 6: employeeDTo.Status = 1; i++; break;
                }

                // Act
                var result = ESC.GetAll(new GetAllRequest(employeeDTo));
                //var result = ESC.Add(employeeDTo);

                // Assert
                Assert.IsTrue(result.GetAllResult.Status, "Cannot search employee to Database");
                //Assert.IsFalse(result.Status, "Cannot insert duplicate employee to Database");
            }
        }

        [TestMethod]
        public void checkSearchEmployeeInCase_SearchNull()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            GetAllRequestDto employeeDTo = new GetAllRequestDto();

            employeeDTo.EmpId = null;
            employeeDTo.Name = null;
            employeeDTo.Status = 1;
            employeeDTo.Sort = null;
            employeeDTo.SortType = null;
            employeeDTo.Status = 1;

            // Act
            var result = ESC.GetAll(new GetAllRequest(employeeDTo));
            //var result = ESC.Add(employeeDTo);

            // Assert
            Assert.IsTrue(result.GetAllResult.Status, "Cannot search employee to Database");
            //Assert.IsFalse(result.Status, "Cannot insert duplicate employee to Database");
        }

        [TestMethod]
        public void checkUpdateEmployeeInCase_CorrectId()
        {
            EmployeeServiceClient ESCS = new EmployeeServiceClient();
            GetAllRequestDto employeeDToS = new GetAllRequestDto();

            var resultS = ESCS.GetAll(new GetAllRequest(employeeDToS));

            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            UpdateRequestDto employeeDTo = new UpdateRequestDto();

            employeeDTo.Id = resultS.GetAllResult.Data[0].Id;
            employeeDTo.Address = "Test Address2";
            employeeDTo.EmpId = "Test Id2";
            employeeDTo.JobTitle = 2;
            employeeDTo.MarriedStatus = 2;
            employeeDTo.Name = "Test Name2";
            employeeDTo.Status = 2;
            employeeDTo.Surname = "Test Surname2";
            employeeDTo.Title = 2;

            // Act
            var result = ESC.Update(new UpdateRequest(employeeDTo));
            //var result = ESC.Update(employeeDTo);

            // Assert
            Assert.IsTrue(result.UpdateResult.Status, "Cannot update employee to Database");
           // Assert.IsTrue(result.Status, "Cannot update employee to Database");
        }

        [TestMethod]
        public void checkUpdateEmployeeInCase_InCorrectId()
        {
            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            UpdateRequestDto employeeDTo = new UpdateRequestDto();

            employeeDTo.Id = 0;
            employeeDTo.Address = "Test Address2";
            employeeDTo.EmpId = "Test Id2";
            employeeDTo.JobTitle = 2;
            employeeDTo.MarriedStatus = 2;
            employeeDTo.Name = "Test Name2";
            employeeDTo.Status = 2;
            employeeDTo.Surname = "Test Surname2";
            employeeDTo.Title = 2;

            // Act
            var result = ESC.Update(new UpdateRequest(employeeDTo));
            //var result = ESC.Update(employeeDTo);

            // Assert
            Assert.IsFalse(result.UpdateResult.Status, "Should cannot update unreal employee to Database");
            //Assert.IsFalse(result.Status, "Should cannot update unreal employee to Database");
        }

        [TestMethod]
        public void checkDeleteEmployeeInCase_CorrectId()
        {
            EmployeeServiceClient ESCS = new EmployeeServiceClient();
            GetAllRequestDto employeeDToS = new GetAllRequestDto();

            var resultS = ESCS.GetAll(new GetAllRequest(employeeDToS));

            // Arrange          
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            DeleteRequestDto employeeDTo = new DeleteRequestDto();

            employeeDTo.IdList = new List<string>();

            String[] arr = new String[1];
            arr[0] = resultS.GetAllResult.Data[0].Id.ToString();

            employeeDTo.IdList.Add(arr[0]);
            //employeeDTo.IdList = arr;

            // Act
            var result = ESC.Delete(new DeleteRequest(employeeDTo));

            // Assert
            Assert.IsTrue(result.DeleteResult.Status, "Cannot delete employee to Database");
        }
    }
}
